$(function() {
	//渠道的编辑弹出框
	$('#channelEditDialog').dialog({
		buttons : [ {
			text : '保存',
			handler : function() {
				if (!$('#channelEditForm').form('validate')) {
					return;
				}
				//验证用户名
				var isVal = 1;
				var username = $("#username").val();
				var userId = $("#userId").val();
				$.ajax({
	 	            type:'post',
	 	            url:'channel/checkUnById.html',
	 	            data:'username='+username+'&id='+userId,
	 	            dataType:'json',
	 	            async: false,
	 	            success:function(r){
	 	            	if (r.r) {
							if(r.count>0){
								isVal=0;
								$.messager.alert('操作提示', '该登陆名已存在！', 'error');
								return false;
							}else{
								isVal=1;
							}
						} else {
							$.messager.alert('操作提示', r.m, 'error');
						}
	 	            }
	            });
				if(isVal==0){
                	return false;
                }
				//验证渠道名称
				var isChannelName = 1;
				var channelName = $("#channelName").val();
				var id = $("#id").val();
;				$.ajax({
	 	            type:'post',
	 	            url:'channel/checkChannelName.html',
	 	            data:'channelName='+channelName+'&id='+id,
	 	            dataType:'json',
	 	            async: false,
	 	            success:function(r){
	 	            	if (r.r) {
							if(r.count>0){
								isChannelName=0;
								$.messager.alert('操作提示', '该渠道名称已存在！', 'error');
								return false;
							}else{
								isChannelName=1;
							}
						} else {
							$.messager.alert('操作提示', r.m, 'error');
						}
	 	            }
	            });
				if(isChannelName==0){
                	return false;
                }
				
				$('#channelEditForm')._ajaxForm(function(r) {
					if (r.r) {
						$('#channelEditDialog').dialog('close');
						$('#grid').datagrid('reload');
					} else {
						$.messager.alert('操作提示', r.m, 'error');
					}
				});
			}
		}, {
			text : '关闭',
			handler : function() {
				$('#channelEditDialog').dialog('close');
			}
		} ]
	});
	var grid = $('#grid')._datagrid({
		checkOnSelect : false,
		selectOnCheck : false,
		toolbar : [{
            text : '创建渠道',
            iconCls : 'icon-add',
            handler : handler_add
        }]
	});
	/*新增渠道*/
    function handler_add() {
        $('#channelEditForm').attr('action','channel/add.html').resetForm();
        $('#id').val('');
        $('#username').removeAttr('readonly');
        $('#channelName').removeAttr('readonly');
        $('#channelEditDialog').dialog('open').dialog("setTitle","新增渠道");
    }
	$('#queryButton').click(function() {
		var params = $('#queryForm')._formToJson();
		$(grid).datagrid('load', params);
	});
});
var formatter = {
	status : function(value, rowData, rowIndex) {
		if (value == 1) {
			return '<font color=green>可登陆</font>';
		} else {
			return '<font color=red>不可登陆</font>';
		}
	},
	batch : function(value, rowData, rowIndex) {
		if (value == 1) {
			return '<font color=green>是</font>';
		} else {
			return '<font color=red>否</font>';
		}
	},
	opt : function(value, rowData, rowIndex) {
		var html = '<a class="spacing a-blue" onclick="updChannel(' + rowIndex+ ');" href="javascript:void(0);">修改</a>';
		html += '<a class="spacing a-green" onclick="resetPassword(' + rowIndex+ ');" href="javascript:void(0);">重置密码</a>';
		return html;
	}
};
/* 修改渠道 */
function updChannel(rowIndex) {
	var data = $('#grid').datagrid('getRows')[rowIndex];
	$('#channelEditForm').attr('action', 'channel/upd.html').resetForm();
	$('#channelName').removeAttr('readonly');
	$('#username').attr('readonly', 'readonly');
	if(data.channelName=='自主渠道'){
		$('#channelName').attr('readonly', 'readonly');
	}
	$('#channelEditForm')._jsonToForm(data);
	shopSubclass=data.shopSubclass;
	shopCity=data.shopCity;
	$('#channelEditDialog').dialog('open').dialog('setTitle', '修改渠道');
}

//商户重置密码
function resetPassword(rowIndex) {
	$.messager.confirm('操作提示', '是否重置密码为‘111111’？', function(r) {
		if (r) {
			var data = $('#grid').datagrid('getRows')[rowIndex];
			$._ajaxPost('channel/resetPassword.html', {
				id : data.id,
				userId : data.userId
			}, function(r) {
				if (r.r) {
					$('#grid').datagrid('reload');
				} else {
					$.messager.alert('操作提示', r.m, 'error');
				}
			});
		}
	});
}


